package com.cskaoyan.catmall.service.Impl;


import com.cskaoyan.catmall.dao.MarketGoodsMapper;
import com.cskaoyan.catmall.dao.MarketTopicMapper;
import com.cskaoyan.catmall.dao.PromotionMapper;
import com.cskaoyan.catmall.model.bo.BatchDeleteTopicIdsBo;
import com.cskaoyan.catmall.model.bo.PromotionTopicBo;
import com.cskaoyan.catmall.model.pojo.MarketGoods;
import com.cskaoyan.catmall.model.pojo.MarketGoodsExample;
import com.cskaoyan.catmall.model.pojo.MarketTopic;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.TopicService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 创建日期: 2022-01-09-20-15
 * author : LL
 * description:
 */

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    PromotionMapper promotionMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketTopicMapper topicMapper;

    @Override
    public PromotionBaseVo<ItemOfPromotionTopicVo> getPromotionTopic(PromotionTopicBo promotionTopicBo) {
        List<ItemOfPromotionTopicVo> promotionTopicVos = promotionMapper.getPromotionTopics(promotionTopicBo);
        PromotionBaseVo<ItemOfPromotionTopicVo> promotionBaseVo = new PromotionBaseVo<>();
        promotionBaseVo.setList(promotionTopicVos);
        return promotionBaseVo;
    }

    @Override
    public void createTopic(MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        topicMapper.insertSelective(marketTopic);
    }

    @Override
    public ItemOfTopicInfoVo getTopicInfo(Integer id) throws JsonProcessingException {
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria();

        ObjectMapper objectMapper = new ObjectMapper();
        String sIds = topicMapper.selectGoodsIdsFromTopic(id);
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] ids = objectMapper.readValue(sIds, Integer[].class);
        List<Integer> idList = Stream.of(ids).collect(Collectors.toList());
        List<MarketGoods> goodsList = null;
        if (idList != null && idList.size() != 0) {

            criteria.andIdIn(idList);
            goodsList = goodsMapper.selectByExample(goodsExample);
        }

        ItemOfTopicInfoVo topicInfoVo = new ItemOfTopicInfoVo();
        topicInfoVo.setTopic(topic);
        topicInfoVo.setGoodsList(goodsList);
        return topicInfoVo;
    }

    @Override
    public void updateTopic(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKey(marketTopic);
    }

    @Override
    public void deleteTopic(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        topicMapper.updateByPrimaryKey(marketTopic);
    }

    @Override
    public void batchDeleteTopicById(BatchDeleteTopicIdsBo topicIdsBo) {
        topicMapper.logicalDeletionById(topicIdsBo.getIds());
    }

}
